package spjm.pt.pmartins.racelogger;

import java.util.ArrayList;
import java.util.HashMap;

import pt.spjm.pmartins.lapcounter.R;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;

public class Setup_AIO extends Activity {

	private ListView lv;
	private Obj_CurrentData cData;
	private Context c;
	private EditText pName, pNumber, rMin, rSec;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setup_aio);
		
		lv = (ListView) findViewById(R.id.listView_aio_setup);
		pName = (EditText) findViewById(R.id.new_pName);
		pNumber = (EditText) findViewById(R.id.new_pNumber);
		rMin = (EditText) findViewById(R.id.race_min);
		rSec = (EditText) findViewById(R.id.race_sec);
		
		cData = LapCounter.cData;
		c = this;
		
		updatePilotsList();
		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, final int arg2, long arg3) {

				Intent intent = new Intent();
				intent.setClass(c,  Setup_Pilots_Edit.class);
				intent.putExtra("id", arg2);
				startActivityForResult(intent, 1);	
			}
		});
	}
	
	public void updatePilotsList(){
		SimpleAdapter sa = new SimpleAdapter(this, loadPilotsList(), R.layout.setup_pilots_list_row, 
				new String[] { "pNumber", "pName" },
				new int[] { R.id.edit_pNumber, R.id.edit_pName });
		lv.setAdapter(sa);
	}
	
	public ArrayList<HashMap<String, String>> loadPilotsList(){
		ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
		HashMap<String, String> row;
		
		if (cData.getPilots().isEmpty()){
			row = new HashMap<String, String>();
			row.put("pNumber", "");
			row.put("pName", "No pilots found");
			list.add(row);
		} else {
			for(Obj_Pilot p : cData.getPilots()){
				row = new HashMap<String, String>();
				row.put("pNumber", "" + p.getNumber());
				row.put("pName", p.getName());
				list.add(row);
			}
		}
		return list;
	}
	
	
	public void onAddPilot(View v){
		String name = "undefined";
		int number = -1;

		try {
			name = pName.getText().toString();
			number = Integer.parseInt(pNumber.getText().toString());

			if (name.equals("") || number < 1) {
				throw new IllegalArgumentException();
			}

			if (cData.getPilots() == null) {
				cData.setPilots( new ArrayList<Obj_Pilot>() );
			}
			cData.getPilots().add( new Obj_Pilot(name, number) );
			
			//showDialogMsg(R.string.new_pilot_success);
			updatePilotsList();
			pName.setText("");
			pNumber.setText("");

		} catch (Exception e) {
			showDialogMsg(R.string.invalid_data);
		}
	}
	
	public void showDialogMsg(final int msg) {
		AlertDialog.Builder bdialog;
		bdialog = new AlertDialog.Builder(this);
		bdialog.setTitle("");
		bdialog.setMessage(msg);
		bdialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {}
		});
		AlertDialog dialog = bdialog.create();
		dialog.show();
	}
	
	public void onDone(View v){
		
		try{
			int min = Integer.parseInt(rMin.getText().toString());
			
			int sec;
			if (rSec.getText().toString().equals("")){
				sec = 0;
			} else {
				sec = Integer.parseInt(rSec.getText().toString());
			}
	
			if (min < 1 || min >= 60 || sec < 0 || sec >= 60) {
				showDialogMsg(R.string.invalid_data);
				return;
			}
			
			cData.getEventsData().setTimeLimit(min + ":" + sec);
			
			Intent intent = new Intent(this, Race.class);
			startActivity(intent);
			
		} catch(Exception e){
			showDialogMsg(R.string.invalid_data);
		}
	}
	
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == 1) {
		     if(resultCode == RESULT_OK){
		    	 updatePilotsList();
		     }
		}
	}
		
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		// getMenuInflater().inflate(R.menu.setup__aio, menu);
		return true;
	}

}
